package com.tan.lbs.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * A filter for rejecting those anonymous user form accessing the web page content.
 * Copyright by Zhensheng Tan only for Final Year Project Use.
 * Class description:
 * @author Zhensheng Tan 
 *
 */
@WebFilter(urlPatterns={"*.jsp","/*Service"})
public class LoginFilter implements Filter
{
	public void destroy()
	{

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest r = (HttpServletRequest) request;

		String requestURI = r.getRequestURI();


		if (requestURI.endsWith("login_designer.jsp")
				|| requestURI.endsWith("DesignerService")
				|| requestURI.endsWith("register_designer.jsp"))
		{
			chain.doFilter(request, response);
			return;
		}

		HttpSession session = r.getSession();

		if (null == session.getAttribute("username"))
		{
			((HttpServletResponse) response).sendRedirect("login_designer.jsp");
			return;
		}
		else
		{
			chain.doFilter(request, response);
		}

	}

	public void init(FilterConfig filterConfig) throws ServletException
	{
		
	}

}

